package cn.wolfcode.car.business.web.controller.system;

import cn.wolfcode.car.base.service.IUserService;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("business/statementItem")
//结算单明细
public class StatementItemController {

    //模板前缀
    private static final String prefix = "business/system/statementItem/";

    @Autowired
    private IStatementService statementService;
    @Autowired
    private IUserService userService;

    @Autowired
    private IStatementItemService statementItemService;
    @RequestMapping("/itemDetail")
    public String editDetail(Model model, Long statementId){
        //通过statementId 获取 服务结算单的数据 并封装到statement对象中
        Statement statement = statementService.get(statementId);
        //服务结算单的对象放在作用域中
        model.addAttribute("statement", statement);
        //判断是消费 还是 已支付的状态
        //如果在消费中跳多editDetail (可编辑)
        if(statement.getStatus() == Statement.STATUS_CONSUME){
            return prefix +"editDetail";
        }else{
            //如果在已支付的状态跳多showDetail (不可编辑)
            //获取收款人的id
            statement.setPayee(userService.get(statement.getPayeeId()));
            return prefix +"showDetail";
        }
    }
    @RequestMapping("/query")
    @ResponseBody
    //左边下显示
    public TablePageInfo<StatementItem> query(StatementItemQuery qo){
        return statementItemService.query(qo);
    }


    @RequestMapping("/saveItems")
    @ResponseBody
    //保存
    public AjaxResult save(@RequestBody List<StatementItem> items){
        statementItemService.save(items);
        return AjaxResult.success();
    }

    @RequestMapping("/payStatement")
    @ResponseBody
    //确认支付
    //在保存那关联收款人ShiroUtils.getUserId()  通过前台传入的id 进行状态修改
    public AjaxResult payStatement(Long statementId){
        statementService.payStatement(statementId);
        return AjaxResult.success();
    }

}